package middleware

import (
	"gin-web/app/model"
	"gin-web/library/errors"
	"gin-web/library/global"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
)

func CheckCasbinAllow(c *gin.Context) {
	value, ok := c.Get("loginUser")
	if !ok {
		c.AbortWithError(401, errors.ErrNoAccess)
	}
	loginUser := value.(*model.User)
	method := c.Request.Method
	path := c.Request.URL.Path
	_, err := global.Casbin.Enforce(loginUser.Workcode, path, method)

	if err != nil {
		c.AbortWithError(401, err)
	}
}
